﻿using System;
using System.Collections;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;
using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using System.Web.Mvc;
using Huirui.Cavan.Domain.Seedwork;
using System.Collections.Generic;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using Newtonsoft.Json;
using System.Linq;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using AutoMapper;

namespace Huirui.Cavan.Presentation.Controller.PurchaseModule
{
    #region PurchaseReturn

    public class PurchaseReturnController : EntityController<PurchaseReturn, string, PurchaseReturnSearch, IPurchaseReturnViewModel, IPurchaseReturnAppService>, IPurchaseReturnController
    {
        public override string ControllerName
        {
            get { return ControllerConst.PURCHASERETURN; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public PurchaseReturnController()
        {
            this.Model = ModelFactory.CreateModel<IPurchaseReturnViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "purchasereturn_query")]
        public ActionResult AddPurchaseReturn()
        {
            return View("~/Views/Purchase/v1/addPurchaseReturn.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "purchasereturn_edit")]
        public JsonResult AddPurchaseReturn(PurchaseReturnViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.StockInOut = new StockInOut();
            IList<PurchaseJson> purchases = JsonConvert.DeserializeObject<IList<PurchaseJson>>(model.Purchases);
            IList<StockInOutDetail> stockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInOutDetails);

            foreach (StockInOutDetail siod in stockInOutDetails)
            {
                siod.TaxRate = model.Item.TaxRate;
            }
            foreach (PurchaseJson pj in purchases)
            {
                model.Item.Purchase = new Purchase() { Id = pj.Id };
                model.Item.StockInOut.StockInOutDetails = stockInOutDetails.Where(s => s.Description == pj.Id).ToList();
                BoolGenericResult<PurchaseReturn> result = this.AppService.AddOrUpdate(model.Item);
                jsonResult = new BoolResult(result.IsSuccess, result.Messages);
            }

            return Json(jsonResult, "application/json");
        }


        [ActionName(ActionConst.LIST)]
        [HttpGet]
        public ActionResult GetPurchaseReturns()
        {
            return View("~/Views/Purchase/v1/purchaseReturnList.cshtml");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        public JsonResult GetPurchaseReturns(PurchaseReturnViewModel model)
        {
            PurchaseReturnJsonViewModel json = new PurchaseReturnJsonViewModel();
            json.rows = new List<PurchaseReturnJson>();

            long total = 0;
            IList<PurchaseReturn> purchaseReturns = this.AppService.GetBySearch(new PurchaseReturnSearch()
            {
                StartDate = model.SearchParam.StartDate,
                EndDate = model.SearchParam.EndDate,
                SupplierCode = model.SearchParam.SupplierCode,
                ID = model.SearchParam.ID
            }, out total).ToList();
            foreach (PurchaseReturn pr in purchaseReturns)
            {
                PurchaseReturnJson cj = Mapper.Map<PurchaseReturn, PurchaseReturnJson>(pr);
                json.rows.Add(cj);
            }
            json.total = total;

            return Json(json, "application/json");
        }
    }
    #endregion
}